import os
import re

import pandas as pd
import qtmodern.styles
import qtmodern.windows
from PySide6.QtCore import Signal, QThread, QSettings
from PySide6.QtWidgets import QWidget, QApplication, QFileDialog
from common import common
from loading import LoadingAnimation
from ui.aa_ui import Ui_Form
from pyfile.GetExcelData import Getdata

class LongTaskThread(QThread):
    finished = Signal()

    def __init__(self,zd):
        super().__init__()
        self.zd=zd

    def run(self):
        try:

            getdata=Getdata(self.zd)
            getdata.getds()

        except Exception as e:
            common.show_message(e, 0)
        finally:
            self.finished.emit()

class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = Ui_Form()
        self.ui.setupUi(self)
        settings = QSettings('MyCompany', 'MyApp')
        self.ui.txt1.setText(settings.value('t1', ''))
        self.ui.txt2.setText(settings.value('t2', ''))
        self.ui.txt3.setText(settings.value('t3', ''))
        self.ui.txt4.setText(settings.value('t4', ''))
        self.ui.txt5.setText(settings.value('t5', ''))
        self.bind()

    def bind(self):

        self.ui.but1.clicked.connect(self.click1)
        self.ui.but2.clicked.connect(self.click2)
        self.ui.but3.clicked.connect(self.click3)
        self.ui.but4.clicked.connect(self.click4)
        self.ui.but5.clicked.connect(self.click5)
        self.ui.pushButton.clicked.connect(self.click)

    def click1(self):
        path = common.open_excel_dialog()
        self.ui.txt1.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t1', path)
    def click2(self):
        path = common.open_excel_dialog()
        self.ui.txt2.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t2', path)
    def click3(self):
        path=common.open_excel_dialog()
        self.ui.txt3.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t3', path)
    def click4(self):
        path=common.open_excel_dialog()
        self.ui.txt4.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t4', path)

    def click5(self):
        path = common.open_excel_dialog()
        self.ui.txt5.setText(path)
        settings = QSettings('MyCompany', 'MyApp')
        settings.setValue('t5', path)

    def click(self):
        self.zd = {'z1': self.ui.txt1.text(), 'z2': self.ui.txt2.text(),
                   'z3': self.ui.txt3.text(), 'z4': self.ui.txt4.text(), 'z5': self.ui.txt5.text()}
        self.ui.longTaskThread = LongTaskThread(self.zd)
        self.ui.finished = self.ui.longTaskThread.finished.connect(self.on_finished)
        current_dir = os.path.dirname(os.path.abspath(__file__))  # 获取当前文件所在目录路径
        self.ui.LoadingAnimation = LoadingAnimation(os.path.join(current_dir, 'gif', '33.gif'))
        self.ui.pushButton.setEnabled(False)
        self.ui.LoadingAnimation.show()
        self.ui.longTaskThread.start()

    def on_finished(self):
        self.ui.pushButton.setEnabled(True)
        self.ui.LoadingAnimation.hide()



if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    qtmodern.styles.dark(app)
    mw = qtmodern.windows.ModernWindow(window)
    mw.move(app.primaryScreen().size().width() / 2 - window.size().width() / 2,
            app.primaryScreen().size().height() / 2 - window.size().height() / 2)
    mw.show()
    app.exec()
